package com.sdp.right.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_import_template_detail.service.ISysImportTemplateDetailService;
import com.sdp.right.entity.SysImportTemplateDetail;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/SysImportTemplateDetailController")
public class SysImportTemplateDetailController {
	
	@Autowired
	private Query query;
	
	@Autowired
	private ISysImportTemplateDetailService iSysImportTemplateDetailService;

	@PostMapping(value = "/list")
	public Result<?> list(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.list(iSysImportTemplateDetailService, SysImportTemplateDetail.class, dataCenter));
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iSysImportTemplateDetailService.removeById(keyId);
		return Result.ok();
	}
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysImportTemplateDetail sysImportTemplateDetail) {
		if(sysImportTemplateDetail==null) {
			return Result.error("字段信息为空");
		}
		
		if(StringUtils.isEmpty(sysImportTemplateDetail.getTemplateId())) {
			return Result.error("字段信息不完整，缺少模板内码");
		}

		if(StringUtils.isEmpty(sysImportTemplateDetail.getKeyId())) {
			iSysImportTemplateDetailService.save(sysImportTemplateDetail);
		}else {
			iSysImportTemplateDetailService.updateById(sysImportTemplateDetail);
		}
	
		return Result.ok();
	}
	
}

